package com.newbee.adapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.List;

import org.json.JSONException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hhu.newbee.R;
import com.newbee.activity.tab.ConfigActivity;
import com.newbee.app.AppConfig;
import com.newbee.app.SpeechApp;
import com.newbee.http.ServerHttpUtil;
import com.newbee.listbean.FriendsInfoBean;
import com.newbee.round.CustomImageView;

public class ConferenceFriendsListvewAdapter extends BaseAdapter
{

	private List<FriendsInfoBean> list;
	private Context context;

	public void setList(List<FriendsInfoBean> list)
	{
		this.list = list;
	}

	public ConferenceFriendsListvewAdapter(List<FriendsInfoBean> list,
			Context context)
	{
		this.list = list;
		this.context = context;
	}

	@Override
	public int getCount()
	{
		if (list != null)
		{
			return list.size();
		}
		else
		{
			return 0;
		}

	}

	@Override
	public Object getItem(int item)
	{
		return item;
	}

	@Override
	public long getItemId(int item)
	{
		// TODO Auto-generated method stub
		return item;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2)
	{
		final LayoutViewBean layout = new LayoutViewBean();

		if (view == null)
		{
			view = LayoutInflater.from(context).inflate(
					R.layout.listitem_conference_friends, null);
		}
		layout.email = (TextView) view
				.findViewById(R.id.conference_friends_email);
		layout.nickName = (TextView) view
				.findViewById(R.id.conference_friends_nickname);
		layout.state = (TextView) view
				.findViewById(R.id.conference_friends_state);

		layout.head = (CustomImageView) view
				.findViewById(R.id.conference_friends_head);
		FriendsInfoBean fib = list.get(position);
		String st = fib.getEmail();

		String[] array = st.split("\\$");

		layout.email.setText("邮箱：" + array[0].replace('#', '@'));
		if (fib.isState())
		{
			layout.state.setText("[在线]");
		}
		else
		{
			layout.state.setText("[离线]");
		}
		try
		{
			String username = (String) SpeechApp.getUserNamePair()
					.get(array[0]);
			layout.nickName.setText("昵称：" + username);
		}
		catch (JSONException e1)
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		final String head = array[2].split("@")[0];
		final File file = new File(Environment.getExternalStorageDirectory()
				+ SpeechApp.HEAD_FILE + "/" + head + ".png");
		// int drawableid = AboutMeActivity.getHeadDrawableID();
		SoftReference<Bitmap> softReference = FriendsListvewAdapter.bitmapMap
				.get(head);
		if (softReference != null)
		{

			Bitmap bitmap = softReference.get();
			if (bitmap != null)
			{
				layout.head.setmSrc(bitmap);
				layout.head.postInvalidate();
			}
			else
			{
				if (file.exists())
				{

					try
					{
						FileInputStream fis = new FileInputStream(file);
						bitmap = BitmapFactory.decodeStream(fis);
						layout.head.setmSrc(bitmap);
						layout.head.postInvalidate();
						fis.close();

						FriendsListvewAdapter.bitmapMap.put(head,
								new SoftReference<Bitmap>(bitmap));
					}
					catch (FileNotFoundException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					catch (IOException e)
					{
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				else
				{
					new Thread(new Runnable()
					{

						public void run()
						{
							try
							{
								InputStream is = new URL(AppConfig.HTTP
										+ ServerHttpUtil.HEAD_dir + head
										+ ".png").openStream();

								Bitmap bitmap = BitmapFactory.decodeStream(is);
								is.close();
								layout.head.setmSrc(bitmap);
								layout.head.postInvalidate();
								FileOutputStream fis = new FileOutputStream(
										file);
								bitmap.compress(Bitmap.CompressFormat.PNG, 100,
										fis);
								fis.close();
								FriendsListvewAdapter.bitmapMap.put(head,
										new SoftReference<Bitmap>(bitmap));
							}
							catch (FileNotFoundException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							catch (IOException e)
							{
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}).start();

				}
			}

		}
		else
		{

			if (file.exists())
			{

				try
				{
					FileInputStream fis = new FileInputStream(file);
					Bitmap bitmap = BitmapFactory.decodeStream(fis);
					layout.head.setmSrc(bitmap);
					layout.head.postInvalidate();
					fis.close();

					FriendsListvewAdapter.bitmapMap.put(head,
							new SoftReference<Bitmap>(bitmap));
				}
				catch (FileNotFoundException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			else
			{
				new Thread(new Runnable()
				{

					public void run()
					{
						try
						{
							InputStream is = new URL(AppConfig.HTTP
									+ ServerHttpUtil.HEAD_dir + head + ".png")
									.openStream();

							Bitmap bitmap = BitmapFactory.decodeStream(is);
							is.close();
							layout.head.setmSrc(bitmap);
							layout.head.postInvalidate();
							FileOutputStream fis = new FileOutputStream(file);
							bitmap.compress(Bitmap.CompressFormat.PNG, 100, fis);
							fis.close();
							FriendsListvewAdapter.bitmapMap.put(head,
									new SoftReference<Bitmap>(bitmap));
						}
						catch (FileNotFoundException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						catch (IOException e)
						{
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}).start();

			}
		}

		return view;
	}

	static class LayoutViewBean
	{
		TextView nickName;
		CustomImageView head;
		TextView email;
		TextView state;

	}

}
